/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Search.java
 *
 * Created on 14/02/2009, 19:25:00
 */

package edu.upf.grupE.interftest;

import edu.upf.grupE.Entities.Picture;
import edu.upf.grupE.Entities.Video;
import edu.upf.grupE.impl.GUILogic;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.table.TableModel;
import org.jivesoftware.smack.RosterEntry;

/**
 *
 * @author Oscar
 */
public class Search extends javax.swing.JPanel {
 private static ArrayList list=new ArrayList();
 private static TableModel dtm;
 private static Downloads downloads;
 private static int row=0;
 private static int searching=0;
 String me,mail;
    /** Creates new form Search */
    public Search(Downloads downloads) {
        this.downloads=downloads;
         //initComponents();

    }

    public void setMe(String me){
        this.me=me;
    }
 public void setMail(String mail){
        this.mail=mail;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        radio_freeText = new javax.swing.JRadioButton();
        btn_search = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();
        radio_ontology = new javax.swing.JRadioButton();
        cmb_ontology = new javax.swing.JComboBox();
        radio_TitleNews = new javax.swing.JRadioButton();
        radio_NewsText = new javax.swing.JRadioButton();
        radio_newsAdjacentParagraph = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jRadioButton1 = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(952, 750));

        buttonGroup1.add(radio_freeText);
        radio_freeText.setSelected(true);
        radio_freeText.setText("Free Search");
        radio_freeText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radio_freeTextActionPerformed(evt);
            }
        });

        btn_search.setText("Search");
        btn_search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_searchActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Results:");

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "File", "Owner", "Size"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jXTable1.setEditable(false);
        dtm= jXTable1.getModel();
        jXTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jXTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jXTable1);

        buttonGroup1.add(radio_ontology);
        radio_ontology.setText("Ontology");
        radio_ontology.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radio_ontologyActionPerformed(evt);
            }
        });

        Vector <String> ontologies= GUILogic.getOntologyes();
        String[] auxiliar= new String[ontologies.size()];
        for(int i=0; i<ontologies.size(); i++){
            cmb_ontology.addItem(ontologies.get(i));
        }

        buttonGroup1.add(radio_TitleNews);
        radio_TitleNews.setText("News Title");

        buttonGroup1.add(radio_NewsText);
        radio_NewsText.setText("News Text");

        buttonGroup1.add(radio_newsAdjacentParagraph);
        radio_newsAdjacentParagraph.setText("News Ajacent Paragraph");

        jLabel2.setText("NEWS");

        jLabel3.setText("SEARCH:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("VIDEOS:");

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Users:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("PICTURES:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(radio_ontology)
                                .addGap(18, 18, 18)
                                .addComponent(cmb_ontology, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(radio_freeText)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(209, 209, 209)
                                        .addComponent(btn_search))
                                    .addComponent(jTextField1)))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 819, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(123, 123, 123)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(radio_TitleNews)
                            .addComponent(radio_NewsText)
                            .addComponent(radio_newsAdjacentParagraph)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addComponent(jRadioButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(74, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_search)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radio_TitleNews)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radio_NewsText)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radio_newsAdjacentParagraph)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                .addComponent(radio_freeText)
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radio_ontology)
                    .addComponent(cmb_ontology, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addGap(28, 28, 28)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jRadioButton1))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(243, 243, 243))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void radio_ontologyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radio_ontologyActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_radio_ontologyActionPerformed

    private void jXTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jXTable1MouseClicked
        // TODO add your handling code here:
          int auxrow= jXTable1.rowAtPoint(evt.getPoint());
      if(searching==1){

            if(evt.getClickCount()==2){
         
         
         String from = (String) jXTable1.getValueAt(auxrow, 1);
        
         Picture aux= (Picture) list.get(auxrow);
         GUILogic.sendPictureRequest(from, aux.getId());
         //jXTable1.g
         downloads.addDown(aux);}}
        else{
            try {
                Video vid = (Video) list.get(auxrow);
                String dir = vid.getFileDescriptor().getPath();
                int port = 55000;
                InetAddress address = InetAddress.getLocalHost();
                String ip = (String) address.getHostAddress();
                //Send message
                GUILogic.sendVideoRequest((String)jComboBox2.getSelectedItem(), ip, vid.getId(), port);
            } catch (UnknownHostException ex) {
                Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
            }

                
                }
            
   
    }//GEN-LAST:event_jXTable1MouseClicked


    
      
      
      private void btn_searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_searchActionPerformed
        // TODO add your handling code here:

        row=0;
        jXTable1.setEnabled(false);
        for(int i=0; i<list.size() || i>=15; i++){
            dtm.setValueAt("", i, 0);
            dtm.setValueAt("", i, 1);
            dtm.setValueAt("", i, 2);
        }
        list.clear();
        downloads.setrow();
        if(radio_TitleNews.isSelected()){
            GUILogic.searchPictureByNewsTitle(me, jTextField1.getText());
            searching=1;
        }
        if(radio_NewsText.isSelected()){
            GUILogic.searchPictureByNewsText(me, jTextField1.getText());
            searching=1;
        }
        if(radio_newsAdjacentParagraph.isSelected()){
            GUILogic.searchPictureByNewsAdjacent(me, jTextField1.getText());
            searching=1;
        }
        if(radio_freeText.isSelected()){
            GUILogic.searchPictureByFreeText(me, jTextField1.getText());
            searching=1;
        }
        if(radio_ontology.isSelected()){
          GUILogic.searchPictureByOntology(me, jTextField1.getText(),(String)cmb_ontology.getSelectedItem());
          searching=1;
        }
         if(jRadioButton1.isSelected()){

          GUILogic.requestVideoList(jComboBox2.getSelectedItem().toString());
          searching=2;
        }

}//GEN-LAST:event_btn_searchActionPerformed

    private void radio_freeTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radio_freeTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_radio_freeTextActionPerformed
   public void PrintResults(Picture pic, String from){
       /*LOGICA DE RECEPCION DE MENSAJES*/
      list.add(pic);
      dtm.setValueAt(pic.getName(), row,0);
      dtm.setValueAt(from, row,1);
      dtm.setValueAt(pic.getFileDescriptor().getSize(), row,2);
      row++;
   }
    public void PrintResults(Video vid, String from){
       /*LOGICA DE RECEPCION DE MENSAJES*/
      jXTable1.setEnabled(true);
      list.add(vid);
      dtm.setValueAt(vid.getName(), row,0);
      dtm.setValueAt(from, row,1);
      dtm.setValueAt(vid.getFileDescriptor().getSize(), row,2);
      row++;
   }

    public void charge(){
        initComponents();
    }

    public void UpdateUsers(){
    if(jComboBox2.getItemCount()>0){
        jComboBox2.removeAllItems();
    }
        Vector<RosterEntry> icontacts = GUILogic.getIlusionOnlineContacts();

    for(int i=0;i<icontacts.size();i++){
    jComboBox2.addItem(icontacts.get(i).getUser());
}
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_search;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cmb_ontology;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private org.jdesktop.swingx.JXTable jXTable1;
    private javax.swing.JRadioButton radio_NewsText;
    private javax.swing.JRadioButton radio_TitleNews;
    private javax.swing.JRadioButton radio_freeText;
    private javax.swing.JRadioButton radio_newsAdjacentParagraph;
    private javax.swing.JRadioButton radio_ontology;
    // End of variables declaration//GEN-END:variables

}
